package com.campusbackend.service.impl;

import com.campusbackend.entity.Awards;
import com.campusbackend.entity.vo.AwardCreateVO;
import com.campusbackend.entity.vo.AwardUpdateVO;
import com.campusbackend.mapper.AwardsMapper;
import com.campusbackend.service.AwardsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 获奖情况表 服务实现类
 * </p>
 *
 * @author yezi
 * @since 2025-07-24
 */
@Service
public class AwardsServiceImpl extends ServiceImpl<AwardsMapper, Awards> implements AwardsService {

    @Autowired
    private AwardsMapper awardsMapper;

    @Override
    public List<Awards> getTeacherAwards(Long teacherId) {
        return awardsMapper.selectByUserTypeAndUserId((byte) 1, teacherId);
    }

    @Override
    public String addTeacherAward(Long teacherId, AwardCreateVO createVO) {
        try {
            Awards award = new Awards();
            BeanUtils.copyProperties(createVO, award);
            
            // 设置用户信息
            award.setUserType((byte) 1); // 1-教师
            award.setUserId(teacherId);
            
            // 设置时间
            award.setCreateTime(LocalDateTime.now());
            award.setUpdateTime(LocalDateTime.now());
            
            boolean saved = save(award);
            if (saved) {
                return "获奖情况添加成功";
            } else {
                return "获奖情况添加失败，请稍后重试";
            }
        } catch (Exception e) {
            return "添加获奖情况时发生错误：" + e.getMessage();
        }
    }

    @Override
    public String updateTeacherAward(Long teacherId, AwardUpdateVO updateVO) {
        try {
            // 首先检查该获奖记录是否属于当前教师
            Awards existingAward = awardsMapper.selectByIdAndUser(updateVO.getId(), (byte) 1, teacherId);
            if (existingAward == null) {
                return "获奖记录不存在或无权限访问";
            }
            
            // 更新获奖信息
            BeanUtils.copyProperties(updateVO, existingAward, "userType", "userId", "createTime");
            existingAward.setUpdateTime(LocalDateTime.now());
            
            boolean updated = updateById(existingAward);
            if (updated) {
                return "获奖情况更新成功";
            } else {
                return "获奖情况更新失败，请稍后重试";
            }
        } catch (Exception e) {
            return "更新获奖情况时发生错误：" + e.getMessage();
        }
    }

    @Override
    public String deleteTeacherAward(Long teacherId, Long awardId) {
        try {
            // 首先检查该获奖记录是否属于当前教师
            Awards existingAward = awardsMapper.selectByIdAndUser(awardId, (byte) 1, teacherId);
            if (existingAward == null) {
                return "获奖记录不存在或无权限访问";
            }
            
            boolean deleted = removeById(awardId);
            if (deleted) {
                return "获奖情况删除成功";
            } else {
                return "获奖情况删除失败，请稍后重试";
            }
        } catch (Exception e) {
            return "删除获奖情况时发生错误：" + e.getMessage();
        }
    }

    @Override
    public List<Awards> getStudentAwards(Long studentId) {
        return awardsMapper.selectByUserTypeAndUserId((byte) 2, studentId);
    }
} 